<template>
  <el-dialog class="enternetValueOpen" :visible.sync="visible" title="录入净值" :close-on-click-modal="false">
    <div class="addNetworth-line" v-for="(item, i) in addNetworthList" :key="i">
      <span>净值时间：</span>
      <el-date-picker :class="{'error-border': errorList.indexOf(i)!=-1 && !item.networthTime}" v-model="item.networthTime" 
        :picker-options="pickerOptions" size="mini" type="date" value-format="yyyy-MM-dd" style="width: 140px;">
      </el-date-picker>
      <span style="padding-left: 20px;">净值：</span><el-input-number :class="{'error-border': errorList.indexOf(i)!=-1 && !item.networthValue }" v-model="item.networthValue" size="mini" :precision="4" style="width: 160px;" :controls="false"></el-input-number>
      <i v-if="i" class="el-icon-close" @click="closeNetworthHandle(i)"></i>
      <i :style="i?'':'margin-left: 20px;'" class="el-icon-plus" @click="addNetworthHandle(i)"></i>
    </div>
    <div slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="submit()">提交</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { insertInvestNetworth, addManageNetWorth } from '@/api/afterInvest' // updateDetail

export default {
  data() {
    return {
      visible: false,
      detailId: '',
      investId: '',
      strategyId: '',
      indexType: '',
      errorList: [],
      addNetworthList: [
        { times: new Date().getTime(), investId: this.investId, detailId: this.detailId, strategyId: this.strategyId, networthTime: '', networthValue: undefined }
      ],
      pickerOptions: {
        disabledDate(time) {
          return time.getTime() > Date.now()
        }
      },
    }
  },
  methods: {
    init({strategyId, detailId, investId, indexType}) {
      this.visible = true
      this.strategyId = strategyId || null
      this.investId = investId || null
      this.indexType = indexType || null
      this.detailId = detailId
      this.addNetworthList = [
        { times: new Date().getTime(), investId: this.investId, detailId: this.detailId, strategyId: this.strategyId, networthTime: '', networthValue: undefined }
      ]
      this.addNetworthList[0].strategyId = strategyId || null
      this.addNetworthList[0].detailId = detailId
    },
    closeNetworthHandle(i) {
      this.addNetworthList.splice(i, 1)
    },
    addNetworthHandle(i) {
      this.addNetworthList.splice(i+1, 0, {
        times: new Date().getTime(), investId: this.investId, detailId: this.detailId, strategyId: this.strategyId, networthTime: '', networthValue: undefined
      })
    },
    submit() {
      this.errorList = []
      this.addNetworthList.map((item, i) => {
        item.networth = item.networthValue
        item.time = item.networthTime
        if(!item.networth || !item.time) {
          this.errorList.push(i)
        }
      })
      if(this.errorList.length) {
        this.$message({
          message: '部分净值录入不完整',
          type: 'warning',
          duration: 2000
        })
        return
      }
      if(this.investId) {
        addManageNetWorth({
          manageNetWorth: this.addNetworthList
        }).then(res => {
          if(res.code == 0) {
            this.$notify({
              message: '添加成功',
              type: 'success',
              duration: 2000
            })
            this.visible = false
            this.$emit('closeOpen')
          }
        })
      }else {
        insertInvestNetworth({
          addNetworthList: this.addNetworthList
        }).then(res => {
          if(res.code == 0) {
            this.$notify({
              message: '添加成功',
              type: 'success',
              duration: 2000
            })
            this.visible = false
            this.$emit('closeOpen')
          }
        })
      }
    },
  }
}
</script>

<style lang="scss" scoped>
.enternetValueOpen {
  ::v-deep .el-dialog__body { max-height: 60vh; overflow-y: auto; }
  .addNetworth-line {
    padding: 0 16px;
    .el-icon-close {
      cursor: pointer;
      color: #ff561b;
      font-weight: bold;
      font-size: 16px;
      margin-left: 20px;
    }
    .el-icon-plus {
      cursor: pointer;
      color: #409EFF;
      font-weight: bold;
      font-size: 16px;
      margin-left: 10px;
    }
    .error-border {
      ::v-deep input { border: 1px solid red; }
    }
  }
  .addNetworth-line + .addNetworth-line {
    margin-top: 12px;
  }
}
</style>
